package cn.dubito.miaosha.validator;

import cn.dubito.miaosha.utils.ValidateUtil;
import cn.dubito.miaosha.validator.annotation.IsMobile;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author liliang
 * @description: 验证器类
 * @date 2018/3/31.
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

    private boolean required = false;

    @Override
    public void initialize(IsMobile isMobile) {
        required = isMobile.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (required) {
            return ValidateUtil.isMobile(s);
        } else {
            return StringUtils.isEmpty(s) || ValidateUtil.isMobile(s);
        }
    }
}
